package com.china08.yxyapi.api.comm;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;

@CrossOrigin
@RestController
public class YunPvUptController {

    @Autowired
    private YxyLessonsCloudLessonRepository yxyLessonsCloudLessonRepository;

    @RequestMapping(value = "/api_yw/comm/yun/pv/upd", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        List<YxyLessonsCloudLesson> cloudLessons = yxyLessonsCloudLessonRepository.findByStatus(2);
        for (YxyLessonsCloudLesson yxyLessonsCloudLesson : cloudLessons) {
            Integer vv = yxyLessonsCloudLesson.getVv();
            if (vv != null) {
                Integer clickPv = yxyLessonsCloudLesson.getClickPv();
                if (clickPv != null) {
                    yxyLessonsCloudLesson.setClickPv(yxyLessonsCloudLesson.getClickPv() + vv);
                } else {
                    yxyLessonsCloudLesson.setClickPv(vv);
                }

            }
            yxyLessonsCloudLessonRepository.save(yxyLessonsCloudLesson);
        }
        RespModel model = new RespModel();
        model.setMessage("更新成功");
        return model;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    public class RespModel extends YxyApiRespModel {

    }
}
